#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>

int map[801][601];
int x[20];
int y[20];
int speedx[100];
int speedy[100];
POINT p;
MOUSEMSG m;
int mousex, mousey;
int gather;

void startup()
{
	initgraph(800, 600);
	map[801][601] = { 0 };
	gather = 0;

	int ballnum = 20;
	int i;
	int a, b;

	for (i = 1; i <= ballnum; i++)
	{
		speedx[i] = 5;
		speedy[i] = 5;
		a = rand() % 781 + 10;
		b = rand() % 581 + 10;
		map[a][b] = 1;
		x[i] = a;
		y[i] = b;
	}

}

void show()
{
	BeginBatchDraw();
	clearrectangle(0, 0, 800, 600);
	int k;
	int m, n;
	for (m = 0; m <= 800; m++)
		for (n = 0; n <= 600; n++)
			map[m][n] = 0;
	for (k = 1; k <= 20; k++)
		map[x[k]][y[k]] = 1;
	int i, j;


	for (i = 0; i <= 800; i++)
		for (j = 0; j <= 600; j++)
		{
			if (map[i][j] == 1)
			{
				setcolor(GREEN);
				setfillcolor(GREEN);
				fillcircle(i, j, 10);
			}

		}
	FlushBatchDraw();
	EndBatchDraw();
}

void withoutinput()
{
	int i;
	for (i = 1; i <= 20; i++)
	{
		if (x[i] + speedx[i] >= 10 && x[i] + speedx[i] <= 790)
			x[i] += speedx[i];
		else
		{
			speedx[i] *= -1;
			x[i] += speedx[i];
		}
		if (y[i] + speedy[i] >= 10 && y[i] + speedy[i] <= 590)
			y[i] += speedy[i];
		else
		{
			speedy[i] *= -1;
			y[i] += speedy[i];
		}
	}
	if (gather == 1)
	{
		int j;

		for (j = 1; j <= 20; j++)
		{
			if (m.x < x[j])
				speedx[j] -= 1;
			else if (m.x > x[j])
				speedx[j] += 1;
			if (m.y < y[j])
				speedy[j] -= 1;
			else if (m.y > y[j])
				speedy[j] += 1;
		}

	}
}




int main()
{
	startup();
	while (1)
	{
		show();
		withoutinput();
	}
	closegraph();
}